package com.gome.ocean.service.authority.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.gome.ocean.common.exception.UserServiceException;
import com.gome.ocean.dao.authority.ModuleDAO;
import com.gome.ocean.dao.model.authority.Module;
import com.gome.ocean.service.authority.ModuleService;

/**
 * Created by liuqingxia on 2016/6/20.
 */
@Service
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private ModuleDAO moduleDAO;

    @Override
    public boolean insertModule(Module module) throws UserServiceException{
    	Map<String, Object> map = new HashMap<String,Object>();
    	map.put("name", module.getName());
    	List<Module> moduleList = moduleDAO.findAllModuleByMap(map);
    	if (moduleList.size()>0) {
    		throw new UserServiceException("模块已经存在");
		}
    	boolean flag = false;
    	try {
			flag = moduleDAO.insertModule(module) > 0;
		} catch (Exception e) {
			throw new UserServiceException(e.toString());
		}
        return flag;
    }

    @Override
    public boolean removeModuleById(Long id) {
        return moduleDAO.removeModuleById(id) == 1;
    }

    @Override
    public boolean updateModule(Module moduleBO) {
        return moduleDAO.updateModule(moduleBO) == 1;
    }

    @Override
    public Module findModuleById(Long id) {
        return moduleDAO.findModuleById(id);
    }

	@Override
	public Page<Module> findAllModule(int pageNum, int pageSize) {
		return (Page<Module>) moduleDAO.findAllModuleByPage(new RowBounds(pageNum, pageSize));
	}

	@Override
	public Module findModuleByMap(Map<String, Object> map) {
		Module module = moduleDAO.findModuleByMap(map);
		return module;
	}

	@Override
	public List<Module> findAllModuleByMap(Map<String, Object> map) {
		try {
			List<Module> list = moduleDAO.findAllModuleByMap(map);
			if (list!=null && list.size()>0) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
